using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 该类由shaco.Execel+Serializable.cs脚本自动生成，请勿手动修改
/// </summary>
namespace ShootMan
{
	public class cfg_hero_attributes : shaco.Base.IExcelData
	{
		public class RowData 
		{
			public System.Int32 ID { get { return _ID; } }
			private System.Int32 _ID;
			public System.String NAME { get { return _NAME; } }
			private System.String _NAME;
			public System.Single MOVE_SPEED { get { return _MOVE_SPEED; } }
			private System.Single _MOVE_SPEED;
			public System.Single SHOOT_SPEED { get { return _SHOOT_SPEED; } }
			private System.Single _SHOOT_SPEED;

			public RowData(System.Int32 ID, 
							System.String NAME, 
							System.Single MOVE_SPEED, 
							System.Single SHOOT_SPEED)
			{
				this._ID = ID;
				this._NAME = NAME;
				this._MOVE_SPEED = MOVE_SPEED;
				this._SHOOT_SPEED = SHOOT_SPEED;
			}

			public RowData() { }

			public RowData Clone()
			{
				var cloneData = new RowData();
				cloneData._ID = this.ID;
				cloneData._NAME = this.NAME;
				cloneData._MOVE_SPEED = this.MOVE_SPEED;
				cloneData._SHOOT_SPEED = this.SHOOT_SPEED;

				return cloneData;
			}
		}

		static public int Count { get {return shaco.Base.ExcelHelper.GetExcelData<cfg_hero_attributes>()._rowDatas.Count;} }
		static public cfg_hero_attributes Instance { get {return shaco.Base.ExcelHelper.GetExcelData<cfg_hero_attributes>();} }
		
		private List<RowData> _rowDatas = new List<RowData>();

		static public RowData Get(System.Int32 index)
		{
			var data = CheckInit();
			if (index < 0 || index > data._rowDatas.Count - 1)
			{
				shaco.Base.Log.Error("cfg_hero_attributes Get error: out of range, index=" + index + " count=" + data._rowDatas.Count);
				return null;
			}
			return data._rowDatas[index];
		}

		static public bool ContainsKey(System.Int32 index)
		{
            var data = CheckInit();
			return index >= 0 && index < data._rowDatas.Count;
        }

		static public void Foreach(System.Func<RowData, bool> callback)
		{
			var data = CheckInit();
            using (var en = data.GetEnumerator())
			{
				while (en.MoveNext())
				{
                    if (!callback(en.Current))
						break;
				}
			}
		}

		public IEnumerator<RowData> GetEnumerator()
		{
			if (0 == _rowDatas.Count) UpdateData();
            using (var en = _rowDatas.GetEnumerator())
			{
				while (en.MoveNext())
				{
					yield return en.Current;
				}
			}
		}

		public void CheckInitAsync(System.Action<float> callbackProgress = null)
		{
            if (0 < _rowDatas.Count)
			{
				if (null != callbackProgress)
					callbackProgress(1.0f);
				return;
			}

            shaco.ExcelHelper.OpenAsync("Assets/ShootMan/ConfigAsset/cfg_hero_attributes.asset", (excelData) =>
			{
				if (null == excelData)
				{
					if (null != callbackProgress)
						callbackProgress(1.0f);
					return;
				}
				
				int rows = excelData.GetTabelDataRowCount();
				int index = 0;

#if UNITY_WEBGL
				shaco.Base.Coroutine.ForeachCount(rows,
#else
                shaco.Base.Coroutine.ForeachCountAsync(rows, 
#endif
				() =>
				{
                    this.UpdateDataOnce(excelData, index++);
					return true;
				}, (percent2) =>
				{
                    if (null != callbackProgress)
					{
                        callbackProgress(percent2 * 0.7f + 0.3f);
					}
#if UNITY_WEBGL
				}, shaco.ExcelHelper.DEFAULT_WEBGL_LOAD_ASYNC_SPEED);
#else
				});
#endif
                }, (percent) =>
			{
				if (null != callbackProgress)
                    callbackProgress(percent * 0.3f);
			});
        }
				
		private void UpdateData()
		{
			var excelData = shaco.ExcelHelper.Open("Assets/ShootMan/ConfigAsset/cfg_hero_attributes.asset");
			if (null != excelData)
			{
				int rows = excelData.GetTabelDataRowCount();
				for (int i = 0; i < rows; ++i)
				{
                    UpdateDataOnce(excelData, i);
				}
                shaco.ExcelHelper.Close(excelData);
			}
		}

		private void UpdateDataOnce(shaco.Base.ExcelData excelData, int i)
		{
            if (!excelData.IsIgnoreRowData(i))
			{
				RowData newData = null;
				try
				{
					var v0 = excelData.GetData(i, 0).ToInt(0, false);
					var v1 = excelData.GetData(i, 1);
					var v2 = excelData.GetData(i, 2).ToFloat(0, false);
					var v3 = excelData.GetData(i, 3).ToFloat(0, false);

					newData = new RowData
					(
						v0,v1,v2,v3
					);
					_rowDatas.Add(newData);
				}
				catch (System.Exception e)
				{
					shaco.Base.Log.Error("[cfg_hero_attributes] UpdateDataOnce error: index=" + i + " e=" + e);
				}
			}
		}

		static private cfg_hero_attributes CheckInit()
		{
            var retValue = shaco.Base.ExcelHelper.GetExcelData<cfg_hero_attributes>();
            if (0 == retValue._rowDatas.Count) 
				retValue.UpdateData();
			return retValue;
		}
	}
}